<?php

/**
 *
 * susan-webui - Susan Web-Frontend
 *
 * @link      https://github.com/susan/susan for the canonical source repository
 * @copyright Copyright (c) 2013-2020 Hechzh Property (http://www.susan.org/)
 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

$title = _('Storage details');
$this->headTitle($title);

?>

<ul class="nav nav-tabs">
   <li class="active"><a href="<?php echo $this->url('storage', array('action'=>'index')); ?>"><?php echo $this->translate('Devices'); ?></a></li>
   <li><a href="<?php echo $this->url('pool', array('action'=>'index')); ?>"><?php echo $this->translate('Pools'); ?></a></li>
   <li><a href="<?php echo $this->url('media', array('action'=>'index')); ?>"><?php echo $this->translate('Volumes'); ?></a></li>
</ul>

<br />

<?php if($this->acl_alert) : echo $this->ACLAlert($this->invalid_commands); elseif(!$this->acl_alert) : ?>

<div class="row">

<!-- modal-001 start -->
<div id="modal-001" class="modal fade modal-001" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
         </button>
         <h4 class="modal-title" id="myModalLabel"><?php echo $this->translate("Director message"); ?></h4>
      </div>
      <div class="modal-body" style="height: 250px; overflow-y: scroll">
         <p><?php echo str_replace(array("\n","\r"), "<br />", $this->result); ?></p>
      </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $this->translate("Close"); ?></button>
      </div>
    </div>
  </div>
</div>
<!-- modal-001 end -->

<!-- modal-002 start -->
<div id="modal-002" class="modal fade modal-002" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
         </button>
         <h4 class="modal-title" id="myModalLabel"><?php echo $this->translate("Label barcodes"); ?></h4>
      </div>
      <div class="modal-body">
         <?php
            $form->prepare();
            $form->setAttribute('class','form-horizontal');
            $form->setAttribute('action', $this->url('storage', array('action' => 'details', 'id' => $this->storagename), array('query' => array('action'=> 'label'))));
            echo $this->form()->openTag($form);
            echo $this->formRow($form->get('storage'));
            echo $this->formRow($form->get('pool'));
            echo '<p class="help-block">'.$this->translate("Assign media to this pool.").'</p>';
            echo $this->formRow($form->get('drive'));
            echo '<p class="help-block">'.$this->translate("Use this drive for labeling.").'</p>';
         ?>
      </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $this->translate("Cancel"); ?></button>
         <?php
            echo $this->formSubmit($form->get('submit')->setAttribute('class','btn btn-primary'));
            echo $this->form()->closeTag($form);
         ?>
      </div>
    </div>
  </div>
</div>
<!-- modal-002 end -->

<div class="col-xs-6 col-md-4">

   <div class="panel panel-default">
      <div class="panel-heading">
         <h3 class="panel-title"><?php echo $this->translate("Import/Export Slots"); ?> - <?php echo $this->storagename; ?></h3>
      </div>
      <div class="panel-body">
         <div id="ie-toolbar">
            <button type="button" class="btn btn-default" id="btn-import-all"><span class="glyphicon glyphicon-import" aria-hidden="true"></span><?php echo $this->translate("Import all"); ?></button>
         </div>
         <table class="table table-no-bordered table-hover" id="ie_slots">
            <thead class="bg-primary">
               <th><?php echo $this->translate("Slot"); ?></th>
               <th><?php echo $this->translate("Volume"); ?></th>
               <th><?php echo $this->translate("Actions"); ?></th>
            </thead>
         </table>
      </div>
   </div>

   <div class="panel panel-default">
      <div class="panel-heading">
         <h3 class="panel-title"><?php echo $this->translate("Drives"); ?> - <?php echo $this->storagename; ?></h3>
      </div>
      <div class="panel-body">
         <table class="table table-no-bordered table-hover" id="drive_slots">
            <thead class="bg-primary">
               <th><?php echo $this->translate("Drive"); ?></th>
               <th><?php echo $this->translate("Loaded"); ?></th>
               <th><?php echo $this->translate("Volume"); ?></th>
               <th><?php echo $this->translate("Actions"); ?></th>
            </thead>
         </table>
      </div>
   </div>

</div>

<div class="col-xs-12 col-md-8">

   <div class="panel panel-default">
      <div class="panel-heading">
         <h3 class="panel-title"><?php echo $this->translate("Slots"); ?> - <?php echo $this->storagename; ?></h3>
      </div>
      <div class="panel-body">
         <div id="slots-toolbar">
            <button type="button" class="btn btn-default" id="btn-update-slots"><span class="glyphicon glyphicon-import" aria-hidden="true"></span><?php echo $this->translate("Update slots"); ?></button>
            <span data-toggle="modal" data-target="#modal-002">
               <button type="button" class="btn btn-default" id="btn-label-barcodes"><span class="glyphicon glyphicon-barcode" aria-hidden="true"></span><?php echo $this->translate("Label barcodes"); ?></button>
            </span>
         </div>
         <table class="table table-no-bordered table-hover" id="normal_slots">
            <thead class="bg-primary">
               <th><?php echo $this->translate("Slot"); ?></th>
               <th><?php echo $this->translate("Volume"); ?></th>
               <th><?php echo $this->translate("Bytes"); ?></th>
               <th><?php echo $this->translate("Last written"); ?></th>
               <th><?php echo $this->translate("Expiration"); ?></th>
               <th><?php echo $this->translate("Status"); ?></th>
               <th><?php echo $this->translate("Media Type"); ?></th>
               <th><?php echo $this->translate("Pool"); ?></th>
               <th></th>
               <th></th>
               <th><?php echo $this->translate("Actions"); ?></th>
            </thead>
            <tbody class="table table-bordered">
            </tbody>
         </table>
      </div>
   </div>

</div>

<?php
   echo $this->headScript()->prependFile($this->basePath() . '/js/custom-functions.js');
   echo $this->headLink()->prependStylesheet($this->basePath() . '/css/bootstrap-table.min.css');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-formatter.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-locale-all.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-cookie.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/moment.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/moment-with-locales.min.js');
?>

<script>

   var basePath = "<?php echo $this->basePath(); ?>";
   var result = '<?php echo str_replace(array("\n","\r"), "", $this->result); ?>';

   function attachImportExportSlotsTable() {
      $('#ie_slots').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         cookie: <?php echo $_SESSION['susan']['dt_statesave']; ?>,
         cookieIdTable: 'ie_slots',
         url: '<?php echo $this->url('storage', array('action' => 'getData'), null) . '?data=statusslots&storage=' . $this->storagename; ?>',
         method: 'get',
         dataType: 'json',
         showRefresh: true,
         sortName: 'slotnr',
         sortOrder: 'asc',
         undefinedText: '',
         columns: [
            {
               field: 'slotnr',
            },
            {
               field: 'volname',
               formatter: function(value, row, index){
                  if(row.content == 'full') {
                     if(row.mr_volstatus != null) {
                        if(row.mr_volstatus != '?') {
                           return formatVolumeName(row.mr_volname, basePath);
                        }
                        else {
                           return row.mr_volname;
                        }
                     }
                  }
               }
            },
            {
               field: 'action',
               formatter: function(value, row, index) {
                  if(row.content == "empty") {
                     return '';
                  }
                  else {
                     return '<span id="import-action"><a class="btn btn-default btn-xs" id="btn-1" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath() . '/storage/details/' . $this->storagename . '?action=import&storage=' . $this->storagename . '&srcslots='; ?>' + row.slotnr + '" title="<?php echo $this->translate("Import"); ?>" id="btn-1"><span class="glyphicon glyphicon-import"></span></a></span>';
                  }
               }
            }
         ],
         toolbar: '#ie-toolbar'
      });
   }

   function attachDriveSlotsTable() {
      $('#drive_slots').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         cookie: <?php echo $_SESSION['susan']['dt_statesave']; ?>,
         cookieIdTable: 'drive_slots',
         url: '<?php echo $this->url('storage', array('action' => 'getData'), null) . '?data=statusslots&storage=' . $this->storagename; ?>',
         method: 'get',
         dataType: 'json',
         showRefresh: true,
         sortName: 'slotnr',
         sortOrder: 'asc',
         undefinedText: '',
         columns: [
            {
               field: 'slotnr',
            },
            {
               field: 'loaded',
            },
            {
               field: 'volname',
               formatter: function(value) {
                  if(value != null) {
                     return formatVolumeName(value, basePath);
                  }
               }
            },
            {
               field: 'type',
               visible: false,
            },
            {
               field: 'action',
               formatter: function(value, row, index) {
                  if(row.content == "empty") {
                     return '';
                  }
                  else {
                     return '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath() . '/storage/details/' . $this->storagename . '?action=release&storage=' . $this->storagename . '&srcslots='; ?>' + row.slotnr + '"title="<?php echo $this->translate("Release"); ?>" id="btn-1"><span class="glyphicon glyphicon-eject"></span></a>';
                  }
               }
            }
         ]
      });
   }

   function attachNormalSlotsTable() {
      $('#normal_slots').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         cookie: <?php echo $_SESSION['susan']['dt_statesave']; ?>,
         cookieIdTable: 'normal_slots',
         url: '<?php echo $this->url('storage', array('action' => 'getData'), null) . '?data=statusslots&storage=' . $this->storagename; ?>',
         method: 'get',
         dataType: 'json',
         pagination : true,
			sidePagination: 'client',
			pageList: [ <?php echo $_SESSION['susan']['dt_lengthmenu']; ?> ],
			pageSize: <?php echo $_SESSION['susan']['dt_pagelength']; ?>,
			search: true,
			showPaginationSwitch: true,
			showRefresh: true,
         sortName: 'slotnr',
         sortOrder: 'asc',
         undefinedText: '',
         columns: [
            {
               field: 'slotnr',
               sortable: true,
            },
            {
               field: 'mr_volname',
               sortable: true,
               formatter: function(value, row, index) {
                  if(row != null) {
                     if(row.mr_volstatus != null) {
                        if(row.mr_volstatus != '?') {
                           return formatVolumeName(row.mr_volname, basePath);
                        }
                        else {
                           return row.mr_volname;
                        }
                     }
                  }
               }
            },
            {
               field: 'mr_volbytes',
               sortable: true,
               formatter: function(value) {
                  if(value == '?') {
                     return '';
                  }
                  else if(value != null) {
                     return formatBytes(value);
                  }
               }
            },
            {
               field: 'mr_lastwritten',
               sortable: true,
               formatter: function(value) {
                  if(value == "" || value == 0 || value == null) {
                     return '';
                  }
                  else {
                     var f = moment.unix(value).format("YYYY-MM-DD HH:mm:ss");
                     return f;
                  }
               }
            },
            {
               field: 'mr_expire',
               sortable: true,
               formatter: function(value, row, index) {
                  if(row.mr_volstatus != "Append") {
                     if(row.mr_expire > row.mr_lastwritten && row.mr_lastwritten > 0) {
                        var i = moment.unix(row.mr_expire).format("YYYY-MM-DD HH:mm:ss");
                        return i;
                     }
                  }
                  return '';
               }
            },
            {
               field: 'mr_volstatus',
               sortable: true,
               formatter: function(value) {
                  if(value == '?') {
                     return '<span class="label label-warning"><?php echo $this->translate("not in catalog"); ?></span>';
                  }
                  else {
                     return value;
                  }
               }
            },
            {
               field: 'mr_mediatype',
               sortable: true,
               formatter: function(value) {
                  if(value == '?') {
                     return '';
                  }
                  else {
                     return value;
                  }
               }
            },
            {
               field: 'pr_name',
               sortable: true,
               formatter: function(value) {
                  if(value == '?') {
                     return '';
                  }
                  else if(value != null) {
                     return '<a href="<?php echo $this->basePath() . '/pool/details/'; ?>'+value+'">'+value+'</a>';
                  }
               }
            },
            {
               field: 'action',
               formatter: function(value, row, index) {
                  if(row.content == "empty") {
                     return '';
                  }
                  else {
                     return '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath() . '/storage/details/' . $this->storagename . '?action=export&storage=' . $this->storagename . '&srcslots='; ?>' + row.slotnr + '" title="<?php echo $this->translate("Export"); ?>" id="btn-1"><span class="glyphicon glyphicon-export"></span></a>';
                  }
               }
            }
         ],
         toolbar: '#slot-toolbar'
      });
   }

   $(document).ready(function() {

      setDtTextDomain('<?php echo $this->basePath() . '/js/locale'; ?>');
      setDtLocale('<?php echo $_SESSION['susan']['locale']; ?>');

      if(result.length > 0) {
         $("#modal-001").modal();
      }

      attachDriveSlotsTable();
      $('#drive_slots').bootstrapTable('filterBy', {type: 'drive'});

      attachImportExportSlotsTable();
      $('#ie_slots').bootstrapTable('filterBy', {type: 'import_slot'});

      attachNormalSlotsTable();
      $('#normal_slots').bootstrapTable('filterBy', {type: 'slot'});

      $('#ie-toolbar').on('click', '#btn-import-all', function() {
         $('#spinner').fadeIn('normal');
         window.location.assign('<?php echo $this->basePath() . "/storage/details/" . $this->storagename . "?action=import&storage=" . $this->storagename; ?>');
      });

      $('#slots-toolbar').on('click', '#btn-update-slots', function() {
         $('#spinner').fadeIn('normal');
         window.location.assign('<?php echo $this->basePath() . "/storage/details/" . $this->storagename . "?action=updateslots&storage=" . $this->storagename; ?>');
      });

      $('#ie_slots').on('click', '#btn-1', function() {
         $('#spinner').fadeTo('normal', 0.50);
      });

      $('#drive_slots').on('click', '#btn-1', function() {
         $('#spinner').fadeTo('normal', 0.50);
      });

      $('#normal_slots').on('click', '#btn-1', function() {
         $('#spinner').fadeTo('normal', 0.50);
      });

   });

</script>

<?php endif; ?>
